<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_main_class extends AWS_CONTROLLER
{	
	public function get_access_rule()
	{
		$rule_action["rule_type"] = "white"; //'black'黑名单,黑名单中的检查  'white'白名单,白名单以外的检查
		$rule_action["actions"] = array();
		
		if (get_setting('guest_explorer') == 'Y')
		{
			$rule_action['actions'][] = 'index';
			$rule_action['actions'][] = 'explore';
		}
		
		return $rule_action;
	}
	
	public function explore_action()
	{
		//分类
		if (TPL::is_output('block/content_category.tpl.htm','home/explore'))
		{
			$content_category = $this->model('module')->content_category();
			
			TPL::assign('content_category', $content_category);
		}
			
		//问题
		if (TPL::is_output('block/content_question.tpl.htm','home/explore'))
		{
			if ($_GET['category'])
			{
				$topic_count = ZCACHE::get('topic_count_category_' . $_GET['category']);
				
				if ($topic_count === false)
				{
					$topic_count = $this->model("question")->get_topic_category_count($_GET['category']);
					
					ZCACHE::set('topic_count_category_' . $_GET['category'], $topic_count, false, get_setting('cache_level_high'));
				}
				
				TPL::assign('topic_count', $topic_count);
			}
			
			if (!$_GET['sort_type'])
			{
				$_GET['sort_type'] = 'new';
			}
			
			$question_list = $this->model('question')->get_discuss_list($_GET['page'], get_setting('contents_per_page'), $_GET['sort_type'], $_GET['topic_id'], $this->user_id, $_GET['category'],$_GET['answer_count'], $_GET['day']);
			
			TPL::assign('question_list', $question_list);
			TPL::assign('question_list_bit', TPL::output('question/ajax/list', false));
		}
		
		//边栏菜单
		if (TPL::is_output('block/sidebar_menu.tpl.htm','home/explore' ))
		{
			TPL::assign('draft_count', $this->model('draft')->get_draft_count('answer', $this->user_id));
			TPL::assign('question_invite_count', $this->model('question')->get_invite_question_list($this->user_id, '', true));
		}	
		
		//边栏最新动态
		if (TPL::is_output('block/sidebar_new_dynamic.tpl.htm','home/explore'))
		{
			$sidebar_new_dynamic = $this->model('module')->sidebar_new_dynamic();
			TPL::assign('sidebar_new_dynamic',$sidebar_new_dynamic);
		
		}
		
		//边栏可能感兴趣的人
		if (TPL::is_output('block/sidebar_recommend_users_topics.tpl.htm','home/explore'))
		{
			$recommend_users_topics = $this->model('module')->recommend_users_topics($this->user_id);
			TPL::assign('sidebar_recommend_users_topics', $recommend_users_topics);
		}
		
		//边栏热门用户
		if (TPL::is_output('block/sidebar_hot_users.tpl.htm','home/explore'))
		{
			$sidebar_hot_users = $this->model('module')->sidebar_hot_user2($this->user_id,$_GET['category']);
			
			TPL::assign('sidebar_hot_users', $sidebar_hot_users);
		}

		//边栏热门话题
		if (TPL::is_output('block/sidebar_hot_topics.tpl.htm','home/explore'))
		{
			$sidebar_hot_topics = $this->model('module')->sidebar_hot_topics($this->user_id,$_GET['category']);
				
			TPL::assign('sidebar_hot_topics', $sidebar_hot_topics);
		}		

		
		if ($_GET['category'] AND $category_info = $this->model('system')->get_category_info($_GET['category']))
		{			
			TPL::assign('category_info', $category_info);
			
			$this->crumb($category_info['title'], '/question/?category=' . $category_info['id']);
			
			$meta_description = $category_info['title'];
			
			if ($category_info['description'])
			{
				$meta_description .=  ' - ' . $category_info['description'];
			}
			
			TPL::set_meta('description', $meta_description);
		}
		
		$this->crumb('发现', '/home/explore/');

		TPL::import_css('css/discussion.css');
		TPL::import_js('js/explore.js');
		
		TPL::output("home/explore");
	}
	
	public function index_action()
	{
		if (! $this->user_id)
		{
			HTTP::redirect('/home/explore/guest');
		}

		//边栏分类
		if (TPL::is_output('block/sidebar_category.tpl.htm','home/index'))
		{
			$sidebar_category = $this->model('module')->sidebar_category();
			TPL::assign('sidebar_category', $sidebar_category);
		}

		//边栏菜单
		if (TPL::is_output('block/sidebar_menu.tpl.htm','home/index' ))
		{
			TPL::assign('draft_count', $this->model('draft')->get_draft_count('answer', $this->user_id));
			TPL::assign('question_invite_count', $this->model('question')->get_invite_question_list($this->user_id, '', true));
		}

		//边栏最新动态
		if (TPL::is_output('block/sidebar_new_dynamic.tpl.htm','home/index'))
		{
			$sidebar_new_dynamic = $this->model('module')->sidebar_new_dynamic();
			TPL::assign('sidebar_new_dynamic',$sidebar_new_dynamic);
		}

		//边栏可能感兴趣的人
		if (TPL::is_output('block/sidebar_recommend_users_topics.tpl.htm','home/index'))
		{	
			$recommend_users_topics = $this->model('module')->recommend_users_topics($this->user_id);
			
			TPL::assign('sidebar_recommend_users_topics', $recommend_users_topics);
		}		
		
		//边栏热门用户
		if (TPL::is_output('block/sidebar_hot_users.tpl.htm','home/index'))
		{
			$sidebar_hot_users = $this->model('module')->sidebar_hot_user($this->user_id);
			TPL::assign('sidebar_hot_users', $sidebar_hot_users);
		}
		
		$this->crumb('首页', '/');
		
		TPL::import_css('css/discussion.css');
		
		TPL::import_js(array(
			'js/LocationSelect.js',
			'js/ajaxupload.js',
			'js/index.js'
		));
		
		TPL::output("home/index");
	}
}